---
category: Tip
created: '2020-04-24'
keywords: addEventListener, attach event handler, detach event handler, removeEventListener
title: Attach event handlers inside other handlers
---

Usually, there are many event handlers which handle different events for different elements. These events could depend on each other.

Let's look at a common use case. When user clicks a button, we will open a modal at the center of screen. The modal can be closed by pressing the Escape key.

There are two handlers here:

-   The first one handles the `click` event of the button
-   The second one handles the `keydown` event of the entire `document`

We often create two separated handlers as following:

```js
const handleClick = function () {
    // Open the modal
};

const handleKeydown = function (e) {
    // Close the modal if the Escape key is pressed
};

// Assume that `buttonEle` represents the button element
buttonEle.addEventListener('click', handleClick);
document.addEventListener('keydown', handleKeydown);
```

The `handleKeydown` handler depends on `handleClick` because we only check the pressed key if the modal is already opened.
It's a common way to add a flag to track if the modal is opened or not:

```js
let isModalOpened = false;

const handleClick = function () {
    // Turn on the flag
    isModalOpened = true;
    // Open the modal ...
};

const handleKeydown = function (e) {
    // Check if the modal is opened first
    if (isModalOpened) {
        // Check the pressed key ...
    }
};
```

More elements, more dependent events and more flags! As the result, it's more difficult to maintain the code.

Instead of adding event separately at first, we add an event handler right inside another one which it depends on.
Here is how the tip approaches:

```js
const handleClick = function () {
    document.addEventListener('keydown', handleKeydown);
};
```

No flag at all! The code is more readable and easier to understand.

## Use cases

You can see the tip used in another posts:

-   [Create a custom scrollbar](https://phuoc.ng/collection/html-dom/create-a-custom-scrollbar/)
-   [Create a range slider](https://phuoc.ng/collection/html-dom/create-a-range-slider/)
-   [Create an image comparison slider](https://phuoc.ng/collection/html-dom/create-an-image-comparison-slider/)
-   [Create resizable split views](https://phuoc.ng/collection/html-dom/create-resizable-split-views/)
-   [Drag to scroll](https://phuoc.ng/collection/html-dom/drag-to-scroll/)
-   [Make a draggable element](https://phuoc.ng/collection/html-dom/make-a-draggable-element/)
-   [Make a resizable element](https://phuoc.ng/collection/html-dom/make-a-resizable-element/)
-   [Print an image](https://phuoc.ng/collection/html-dom/print-an-image/)
-   [Resize columns of a table](https://phuoc.ng/collection/html-dom/resize-columns-of-a-table/)
-   [Show a custom context menu at clicked position](https://phuoc.ng/collection/html-dom/show-a-custom-context-menu-at-clicked-position/)
-   [Show or hide table columns](https://phuoc.ng/collection/html-dom/show-or-hide-table-columns/)

## See also

-   [Attach or detach an event handler](https://phuoc.ng/collection/html-dom/attach-or-detach-an-event-handler/)
-   [Create one time event handler](https://phuoc.ng/collection/html-dom/create-one-time-event-handler/)
